package com.amtf.datafile3.core.excel.parser;

import java.text.DateFormat;
import java.text.Format;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;

import com.amtf.datafile3.DataException;
import com.amtf.datafile3.DataFieldDefinition;
import com.amtf.datafile3.DataFieldParser;
import com.amtf.datafile3.DataFieldRender;
import com.amtf.datafile3.DataFieldType;
import com.amtf.datafile3.DataRecord;
import com.amtf.datafile3.DataRecordParser;
import com.amtf.datafile3.DataTableDefinition;
import com.amtf.datafile3.cfg.excel.ExcelDataFieldDefinition;
import com.amtf.datafile3.cfg.std.StdDataFieldDefinition;
import com.amtf.datafile3.core.std.AbsDataRecordParser;
import com.amtf.datafile3.core.std.render.StringFieldRender;
import com.amtf.datafile3.core.std.type.DateFieldType;
import com.amtf.datafile3.core.std.type.DecimalFieldType;
import com.amtf.datafile3.core.std.type.ObjectFieldType;
import com.amtf.datafile3.core.std.type.StringFieldType;

public class ExcelDataRecordParser extends AbsDataRecordParser<Row, Cell> {
	private int firstColumn;
	private FormulaEvaluator evaluator;

	public ExcelDataRecordParser(FormulaEvaluator evaluator, int firstColumn) {
		this.firstColumn = firstColumn;
		this.evaluator = evaluator;
	}

	public DataRecord parse(DataTableDefinition definition, Row record) throws DataException {
		Row row = (Row) record;

		DataRecord rec = definition.newDataRecordInstance();

		Object[] values = new Object[definition.getFieldCount()];
		for (int i = 0; i < values.length; i++) {
			Cell cell = row.getCell(i + firstColumn);
			values[i] = cell;
		}

		return rec.parse(values);
	}

	public DataRecord parse(DataTableDefinition definition, Cell[] recordFields) throws DataException {
		DataRecord rec = definition.newDataRecordInstance();
		return rec.parse(recordFields);
	}

	public void detectColumn(DataTableDefinition definition, Row titleRow, boolean isTitle) throws DataException {
		if (titleRow != null) {
			Row row = (Row) titleRow;
			if (definition.getFieldCount() == 0) {
				int i = firstColumn;
				Cell cell = row.getCell(i);
				while (cell != null) {
					ExcelDataFieldDefinition fd = new ExcelDataFieldDefinition(isTitle ? cell.toString() : null, new StringFieldType(), false, null, null, evaluator);
					definition.addField(fd);
					cell = row.getCell(++i);
				}
			} else if (isTitle) {
				for (int i = 0; i < definition.getFieldCount(); i++) {
					DataFieldDefinition fielddef = definition.getField(i);
					((ExcelDataFieldDefinition) fielddef).setEvaluator(evaluator);
					// ((AbsExcelDataFieldParser) fielddef.getParser()).setEvaluator(evaluator);
					if (fielddef.getName().length() == 0) {
						Cell cell = row.getCell(i + firstColumn);
						if (cell != null) {
							fielddef.setName(cell.getStringCellValue());
						}
					}
				}
			}
		}
	}

	@Override
	public DataRecordParser<Row, Cell> clone() {
		ExcelDataRecordParser newObj = new ExcelDataRecordParser(evaluator, firstColumn);
		return newObj;
	}

	public void resetColumnLike(DataTableDefinition fromDefinition, DataTableDefinition toDefinition) {
		toDefinition.removeAll();
		for (int i = 0; i < fromDefinition.getFieldCount(); i++) {
			DataFieldDefinition fieldDef = fromDefinition.getField(i);

			DataFieldType type = fieldDef.getType().clone();
			DataFieldRender render = new StringFieldRender();
			DataFieldParser parser = fieldDef.getParser();

			ExcelDataFieldDefinition fd = new ExcelDataFieldDefinition(fieldDef.getName(), type, false, parser.getFormat(), render.getFormat(), evaluator);
			toDefinition.addField(fd);
		}
	}

	public DataRecord parse(DataTableDefinition definition, DataRecord record) throws DataException {
		DataRecord newrecord = definition.newDataRecordInstance();
		newrecord.set(record.getValues());
		return newrecord;
	}

	public FormulaEvaluator getEvaluator() {
		return evaluator;
	}

	public void setEvaluator(FormulaEvaluator evaluator) {
		this.evaluator = evaluator;
	}

	public int getFirstColumn() {
		return firstColumn;
	}

	public void setFirstColumn(int firstColumn) {
		this.firstColumn = firstColumn;
	}

	// public static String toValue(Cell cell, FormulaEvaluator evaluator, DataFieldrending rending) {
	// switch (cell.getCellType()) {
	// case Cell.CELL_TYPE_NUMERIC:
	// if (HSSFDateUtil.isCellDaterendingted(cell)) { // 判断是日期类型
	// Date dt = cell.getDateCellValue();
	// return rending.rending(dt);
	// } else {
	// return new BigDecimal(cell.getNumericCellValue()).toPlainString();
	// }
	// case Cell.CELL_TYPE_STRING:
	// return cell.getStringCellValue();
	// case Cell.CELL_TYPE_BLANK:
	// return "";
	// case Cell.CELL_TYPE_BOOLEAN:
	// return String.valueOf(cell.getBooleanCellValue());
	// case Cell.CELL_TYPE_FORMULA:
	// CellValue cellValue = evaluator.evaluate(cell);
	// switch (cellValue.getCellType()) {
	// case Cell.CELL_TYPE_NUMERIC:
	// if (HSSFDateUtil.isCellDaterendingted(cell)) { // 判断是日期类型
	// Date dt = cell.getDateCellValue();
	// return rending.rending(dt);
	// } else {
	// return new BigDecimal(cell.getNumericCellValue()).toPlainString();
	// }
	// case Cell.CELL_TYPE_STRING:
	// return cell.getStringCellValue();
	// case Cell.CELL_TYPE_BOOLEAN:
	// return String.valueOf(cell.getBooleanCellValue());
	// case Cell.CELL_TYPE_BLANK:
	// return "";
	// }
	// }
	//
	// return cell.toString();
	// }

	// private int testColumnCount(Row row) {
	// int i = firstColumn;
	// Cell cell = row.getCell(i);
	// while (cell != null) {
	// cell = row.getCell(++i);
	// }
	//
	// return i - firstColumn;
	// }

}
